package com.pudu.pudusclient;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.pudu.mydemo.entity.FileUpload;
import com.pudu.pdrobot.App;
import com.pudutech.pdrobot.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    TextView mtxAllMileage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.debug_activity_main);

//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);

        Button btn1 = (Button)this.findViewById(R.id.button1);
        Button btn2 = (Button)this.findViewById(R.id.button2);

        Button btUploadFile = (Button)this.findViewById(R.id.btUploadFile);
        Button button4 = (Button)this.findViewById(R.id.button4);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btUploadFile.setOnClickListener(this);
        button4.setOnClickListener(this);

        try {
            mtxAllMileage = (TextView) this.findViewById(R.id.textView2);
            int nTotalMile = App.totalMiles;

            double dMileage =  (double)nTotalMile/(double)1000/(double)100;
            String strMileage = String.format(this.getString(R.string.robot_total_mileage), dMileage);
            mtxAllMileage.setText(strMileage);

            Message msg = mHandler.obtainMessage();
            msg.what= 0x01;
            mHandler.sendMessageDelayed(msg, 2000);
        }catch(Exception e)
        {
            e.printStackTrace();
        }

    }

    public void SetAllMileage()
    {
        try {
            mtxAllMileage = (TextView) this.findViewById(R.id.textView2);
            int nTotalMile = App.totalMiles;

            double dMileage =  (double)nTotalMile/(double)1000/(double)100;
            String strMileage = String.format(this.getString(R.string.robot_total_mileage), dMileage);
            mtxAllMileage.setText(strMileage);

        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch(v.getId())
        {
            case R.id.button1:
                intent = new Intent(this, ServerAct.class);
                startActivity(intent);
                break;
            case R.id.button2:
                intent = new Intent(this, ClientAct.class);
                startActivity(intent);
                break;
            case R.id.btUploadFile:
                FileUpload fp = new FileUpload();
                fp.uploadFile(v.getContext());
                break;
            case R.id.button4:
                intent = new Intent(this, ShowRadarActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }
    public Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what)
            {
                case 0x01:
                    try {
                        MainActivity.this.SetAllMileage();
                        Message nextmsg = mHandler.obtainMessage();
                        nextmsg.what = 0x01;
                        mHandler.sendMessageDelayed(nextmsg, 2000);
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                    break;
                default:
                    break;
            }
        }
    };

}
